<?php
/**
 * MetadataController
 *
 * @author fusq
 */
require_once BaseUtil::getModulePath ( "base" ) . '/controllers/BaseController.php';
require_once BaseUtil::getModulePath ( "metadata" ) . '/forms/SetForm.php';
require_once BaseUtil::getModulePath ( "metadata" ) . '/forms/GroupForm.php';
require_once BaseUtil::getModulePath ( "metadata" ) . '/forms/MetadataForm.php';
require_once BaseUtil::getModulePath ( "metadata" ) . '/services/SetService.php';

class Metadata_SetController extends BaseController {
	
	public function init() {
		$this->view->script = array ('lib/dhtmlXCommon.js', 'lib/dhtmlXTree.js', 'lib/dhtmlXTreeExtend.js', 'metadataset.js', 'lib/ui.dialog.js', "metadata.js" );
		$this->view->link = array ('dhtmlXTree.css' );
		parent::init ();
	}
	
	public function indexAction() {
		$servcie = new SetService ( );
		$grid = $servcie->getSets ( $this->view, $_POST );
		$this->view->dbGrid = $grid;
	}
	
	public function addAction() {
		$servcie = new SetService ( );
		$setList = $servcie->getSetList ();
		$this->view->setList = $setList;
		$this->view->action = HttpUtil::getUrl ( "set", "subadd" );
	}
	
	public function subaddAction() {
		//$this->_helper->layout->disableLayout();
		$service = new SetService ( );
		switch ($_POST ['addSetFrom']) {
			case 'addSet' :
				$this->_redirector->gotoUrl ( '/metadata/set/edit' );
				break;
			case 'addSetFromExist' :
				$id = $_POST ['ExistSet'];
				if ($id > 0) {
					$setId = $service->addSetDataById ( $id );
					$this->_redirector->gotoUrl ( '/metadata/set/edit/id/' . $setId );
				} else {
					$this->_redirector->gotoUrl ( '/metadata/set/edit' );
				}
				
				break;
		}
		exit ();
	}
	
	public function addsetAction() {
		$this->_helper->layout->disableLayout ();
		$form = new SetForm ( );
		$this->view->form = $form;
	}
	public function addgroupAction() {
		$this->_helper->layout->disableLayout ();
		$form = new GroupForm ( );
		
		$service = new SetService ( );
		$setId = str_replace ( 'set_', '', $_POST ['parentId'] );
		$order = $service->getMaxOrderOfGroup ( $setId );
		$form->getElement ( 'sort_order' )->setValue ( $order );
		$this->view->form = $form;
	}
	
	public function addmetadataAction() {
		$this->_helper->layout->disableLayout ();
		$form = new MetadataForm ( );
                $form->getElement ( "valueRequired" )->setValue ("0");
		$form->getElement ( "inBasicSearch" )->setValue ("1");
		$form->getElement ( "inAdvanceSearch" )->setValue ("1");
		$form->getElement ( "showInProduct" )->setValue ("1");
		$service = new SetService ( );
		$id = str_replace ( 'group_', '', $_POST ['parentId'] );
		$colors = $service->getColors ();
		$order = $service->getMaxOrderOfMetadata ( $id );
		$form->getElement ( 'sortOrder' )->setValue ( $order );
		$this->view->form = $form;
		$this->view->colors = $colors;
	}
	
	public function subaddgroupAction() {
		$this->_helper->layout->disableLayout ();
		$service = new SetService ( );
		$form = new GroupForm ( );
		if (! $form->isValid ( $_POST )) {
			$this->view->form = $form;
			return $this->render ( 'addgroup' );
		}
		$data = array ();
		$data ['title'] = $form->getValue ( "title" );
		$data ['sort_order'] = $form->getValue ( "sort_order" );
		$data ['metadata_set_id'] = str_replace ( 'set_', '', $_POST ["parentId"] );
		$setId = $service->addGroupData ( $data );
		//$service->addSetMetas ( $_POST ['metadata_id'], $setId );
		echo 'ok';
		exit ();
	}
	public function subaddsetAction() {
		$this->_helper->layout->disableLayout ();
		$service = new SetService ( );
		$form = new SetForm ( );
		if (! $form->isValid ( $_POST )) {
			$this->view->form = $form;
			return $this->render ( 'addset' );
		}
		$data = array ();
		$data ['title'] = $form->getValue ( "title" );
		$data ['tag'] = $form->getValue ( "tag" );
		$data ['default_cheque_rule'] = $form->getValue ( "default_cheque_rule" );
		$data ['default_price_rule'] = $form->getValue ( "default_price_rule" );
		$data ['default_ship_rule'] = $form->getValue ( "default_ship_rule" );
		$data ['default_related_rule'] = $form->getValue ( "default_related_rule" );
		$setId = $service->addSetData ( $data );
		echo $setId;
		exit ();
	}
	public function subaddmetadataAction() {
		$this->_helper->layout->disableLayout ();
		$form = new MetadataForm ( );
		$service = new SetService ( );
		if (! $form->isValid ( $_POST )) {
			$this->view->form = $form;
			$colors = $service->getColors ();
			
			$this->view->colors = $colors;
			return $this->render ( 'addmetadata' );
		}
		
		$service->saveMetadata ( $form, $_POST );
		echo 'ok';
		exit ();
	}
	public function editAction() {
		//$this->_helper->layout->disableLayout();
		if ($this->_hasParam ( 'id' )) {
			$id = $this->_getParam ( 'id' );
			$servcie = new SetService ( );
			$set = $servcie->getSetById ( $id );
			$xml = $servcie->getSetTreeXml ( $id );
			$this->view->id = $id;
			$this->view->xml = $xml;
		}
	}
	
	public function subeditAction() {
		$service = new SetService ( );
		$form = new SetForm ( );
		if (! $form->isValid ( $_POST )) {
			$this->view->form = $form;
			return $this->render ( 'add' );
		}
		$data = array ();
		$setId = $form->getValue ( "id" );
		$data ['title'] = $form->getValue ( "title" );
		$data ['tag'] = $form->getValue ( "tag" );
		$data ['default_cheque_rule'] = $form->getValue ( "default_cheque_rule" );
		$data ['default_price_rule'] = $form->getValue ( "default_price_rule" );
		$data ['default_ship_rule'] = $form->getValue ( "default_ship_rule" );
		$data ['default_related_rule'] = $form->getValue ( "default_related_rule" );
		$service->updateSetData ( $data, $setId );
		$service->updateSetMetas ( $_POST ['metadata_id'], $setId );
		$this->_redirector->gotoUrl ( 'metadata/set' );
	}
	public function searchrelateAction() {
		$this->_helper->layout->disableLayout ();
		$servcie = new SetService ( );
		$grid = $servcie->getSetMetaDatas ( $this->view, $_POST );
		$this->view->dbGrid = $grid;
	}
	
	public function deletesetAction() {
		$id = $this->_getParam ( 'id' );
		$servcie = new SetService ( );
		$servcie->deleteSetData ( $id );
		exit ();
	}
	public function deletegroupAction() {
		$id = $this->_getParam ( 'id' );
		$servcie = new SetService ( );
		$servcie->deleteGroup ( $id );
		exit ();
	}
	public function deletemetadataAction() {
		$id = $this->_getParam ( 'id' );
		$service = new SetService ( );
		$service->deleteMetadata ( $id );
		exit ();
	}
	public function editsetAction() {
		
		$this->_helper->layout->disableLayout ();
		$id = $this->_getParam ( 'id' );
		$service = new SetService ( );
		$set = $service->getSetById ( $id );
		$form = new SetForm ( $set );
		$this->view->form = $form;
	}
	
	public function subeditsetAction() {
		$this->_helper->layout->disableLayout ();
		$service = new SetService ( );
		$setId = $_POST ["id"];
		$set = $service->getSetById ( $setId );
		$form = new SetForm ( $set );
		if (! $form->isValid ( $_POST )) {
			$this->view->form = $form;
			return $this->render ( 'editset' );
		}
		$data = array ();
		$setId = $form->getValue ( "id" );
		$data ['title'] = $form->getValue ( "title" );
		$data ['tag'] = $form->getValue ( "tag" );
		$data ['default_cheque_rule'] = $form->getValue ( "default_cheque_rule" );
		$data ['default_price_rule'] = $form->getValue ( "default_price_rule" );
		$data ['default_ship_rule'] = $form->getValue ( "default_ship_rule" );
		$data ['default_related_rule'] = $form->getValue ( "default_related_rule" );
		$service->updateSetData ( $data, $setId );
		echo 'ok';
		exit ();
	}
	
	public function editgroupAction() {
		
		$this->_helper->layout->disableLayout ();
		$id = $this->_getParam ( 'id' );
		$service = new SetService ( );
		$group = $service->getGroupById ( $id );
		$form = new GroupForm ( $group );
		$this->view->form = $form;
	}
	public function subeditgroupAction() {
		$this->_helper->layout->disableLayout ();
		$service = new SetService ( );
		$form = new GroupForm ( );
		
		if (! $form->isValid ( $_POST )) {
			$this->view->form = $form;
			return $this->render ( 'editgroup' );
		}
		$groupId = $form->getValue ( "id" );
		$data = array ();
		$data ['title'] = $form->getValue ( "title" );
		$data ['sort_order'] = $form->getValue ( "sort_order" );
		//$data ['metadata_set_id'] = str_replace('set_','',$_POST ["parentId" ]);
		$setId = $service->updateGroupData ( $data, $groupId );
		//$service->addSetMetas ( $_POST ['metadata_id'], $setId );
		echo 'ok';
		exit ();
	}
	public function editmetadataAction() {
		$this->_helper->layout->disableLayout ();
		$id = $this->_getParam ( "id" );
		$metadata = array ();
		$metadataOption = array ();
		$service = new SetService ( );
		$colors = $service->getColors ();
		$metadata = $service->getMetadata ( $id );
		$metadataOption = $service->getOptionsByMetadataId ( $metadata ['id'] );
		$metadataForm = new MetadataForm ( $id );
		$metadataForm->getElement ( "id" )->setValue ( $metadata ['id'] );
		$metadataForm->getElement ( "inputTypeValue" )->setValue ( $metadata ['input_type'] );
		$metadataForm->getElement ( "code" )->setValue ( $metadata ['code'] );
		$metadataForm->getElement ( "title" )->setValue ( $metadata ['title'] );
		$metadataForm->getElement ( "longTitle" )->setValue ( $metadata ['long_title'] );
		$metadataForm->getElement ( "inputType" )->setValue ( $metadata ['input_type'] );
		$metadataForm->getElement ( "valueRequired" )->setValue ( $metadata ['value_required'] );
		$metadataForm->getElement ( "description" )->setValue ( $metadata ['description'] );
		$metadataForm->getElement ( "inBasicSearch" )->setValue ( $metadata ['in_basic_search'] );
		$metadataForm->getElement ( "inAdvanceSearch" )->setValue ( $metadata ['in_advanced_search'] );
		$metadataForm->getElement ( "isPrimary" )->setValue ( $metadata ['is_primary'] );
		$metadataForm->getElement ( "showInProduct" )->setValue ( $metadata ['show_in_product'] );
		$metadataForm->getElement ( "sortOrder" )->setValue ( $metadata ['sort_order'] );
		$metadataForm->getElement ( "level" )->setValue ( $metadata ['type'] );
		$this->view->form = $metadataForm;
		$this->view->metadataOption = $metadataOption;
		$this->view->metadata = $metadata;
		$this->view->id = $id;
		$this->view->colors = $colors;
	}
	
	public function subeditmetadataAction() {
		$this->_helper->layout->disableLayout ();
		$id = $_POST ['id'];
		$metadataForm = new MetadataForm ( $id );
		$service = new SetService ( );
		if (! $metadataForm->isValid ( $_POST )) {
			$colors = $service->getColors ();
			$metadata = $service->getMetadata ( $id );
			$metadataOption = $service->getOptionsByMetadataId ( $id );
			$this->view->metadataOption = $metadataOption;
			$this->view->metadata = $metadata;
			$this->view->form = $metadataForm;
			$this->view->id = $id;
			$this->view->colors = $colors;
			return $this->render ( 'editmetadata' );
		}
		$service->updateMetadata ( $metadataForm, $_POST );
		echo 'ok';
		exit ();
	}
	
	public function gettreexmlAction() {
		$id = $this->_getParam ( 'id' );
		$service = new SetService ( );
		$xml = $service->getSetTreeXml ( $id );
		echo $xml;
		exit ();
	}
}
